<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <!-- 文本框和按钮 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList()"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goodsAdd()">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 表格 -->
      <el-table :data="goodsList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column
          label="商品名称"
          prop="goods_name"
          width="400px"
        ></el-table-column>
        <el-table-column label="商品价格(元)" prop="goods_price" width="110px">
        </el-table-column>
        <el-table-column
          label="商品重量(克)"
          prop="goods_weight"
          width="110px"
        ></el-table-column>
        <el-table-column label="创建时间" prop="add_time">
          <template slot-scope="scoped">
            {{ scoped.row.add_time | dataFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200px">
          <template slot-scope="scoped">
            <el-button type="primary" icon="el-icon-edit" size="mini"
              >编辑</el-button
            >
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeById(scoped.row.goods_id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        //查询参数
        query: '',
        //当前页码
        pagenum: 1,
        //每条显示数
        pagesize: 5,
      },
      //存储商品列表
      goodsList: [],

      total: 0,
    }
  },
  methods: {
    //根据分页获取对应的商品列表
    async getGoodsList() {
      const { data: res } = await this.$http.get('goods', {
        params: this.queryInfo,
      })
      if (res.meta.status !== 200) {
        this.$message.error('商品列表获取失败！！')
      }
      // console.log(res);
      //商品列表
      this.goodsList = res.data.goods
      //ta
      this.total = res.data.total
      // console.log(this.total);
    },
    //页码显示数发生改变
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    //当前页码发生改变
    handleCurrentChange(newNum) {
      this.queryInfo.pagenum = newNum
      this.getGoodsList()
    },
    //删除
  async removeById(id) {
    console.log(id);
    let decision=await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).catch(error=>error);
     if (decision==='cancel') {
       return this.$message.error('已取消删除');
     }
    const {data:res}= await this.$http.delete(`goods/${id}`);
    // console.log(res);
    if (res.meta.status!==200) {
       return this.$message.error('删除失败！！');      
    }
    this.getGoodsList();
    },
    //添加商品,跳转至添加商品页面
    goodsAdd(){
       this.$router.push('/goods/add');
    },
  },
  created() {
    this.getGoodsList()
  },
}
</script>
<style scoped>
</style>